/*
 *  Copyright 2009 Allan Lykke Christensen.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package dk.i2m.netbeans.modules.doxia;

import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.text.Document;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.openide.text.CloneableEditor;
import org.openide.text.NbDocument;
import org.openide.util.Lookup;
import org.openide.util.Mutex;
import org.openide.windows.TopComponent;

/**
 *
 * @author Allan Lykke Christensen
 */
public class AptSourceEditor extends CloneableEditor implements MultiViewElement,
        Runnable {

    private JComponent toolbar;
    private MultiViewElementCallback callback;

    public AptSourceEditor() {
    }

    AptSourceEditor(AptEditorSupport ed) {
        super(ed);
    }

    public JComponent getVisualRepresentation() {
        return this;
    }

    // Return the editor's custom toolbar,
    // so our toggle button could integrate
    // with it:
    public JComponent getToolbarRepresentation() {
        if (toolbar == null) {
            JEditorPane pane = this.pane;
            if (pane != null) {
                Document doc = pane.getDocument();
                if (doc instanceof NbDocument.CustomToolbar) {
                    toolbar = ((NbDocument.CustomToolbar) doc).createToolbar(
                            pane);
                }
            }
            if (toolbar == null) {
                // attempt to create own toolbar?
                toolbar = new JPanel();
            }
        }
        return toolbar;
    }

    public void setMultiViewCallback(MultiViewElementCallback callback) {
        this.callback = callback;
        updateName();
    }

    @Override
    public void componentOpened() {
        super.componentOpened();
    }

    @Override
    public void componentClosed() {
        super.componentClosed();
    }

    @Override
    public void componentShowing() {
        super.componentShowing();
    }

    @Override
    public void componentHidden() {
        super.componentHidden();
    }

    @Override
    public void componentActivated() {
        super.componentActivated();
    }

    @Override
    public void componentDeactivated() {
        super.componentDeactivated();
    }

    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;
    }

    @Override
    public void updateName() {
        Mutex.EVENT.readAccess(this);
    }

    public void run() {
        MultiViewElementCallback c = callback;
        if (c == null) {
            return;
        }
        TopComponent tc = c.getTopComponent();
        if (tc == null) {
            return;
        }
        super.updateName();
        tc.setName(this.getName());
        tc.setDisplayName(this.getDisplayName());
        tc.setHtmlDisplayName(this.getHtmlDisplayName());
    }

    @Override
    public Lookup getLookup() {
        return ((AptDataObject) ((AptEditorSupport) cloneableEditorSupport()).
                getDataObject()).getNodeDelegate().getLookup();
    }
}
